#include "GlDxGraphicComponent.h"

#include "GlSpriteRenderer.h"

namespace GraphicLibrary
{
	//---------------------------------------------------
	DxGraphicComponent::DxGraphicComponent()
		: mVisibility(true)
		, mLayer(0.0f)
		, mRotation(0.0f)
		, mTypeInfo(0)
		, mSelfPointer(0)
	{
		SpriteRenderer::GetSingleton()->add(this);
	}

	//---------------------------------------------------
	DxGraphicComponent::~DxGraphicComponent()
	{
		SpriteRenderer::GetSingleton()->remove(this);
	}

	//---------------------------------------------------
	void DxGraphicComponent::move(const Position &newPos)
	{
		mPos = newPos;
	}

	//---------------------------------------------------
	float DxGraphicComponent::getX() const
	{
		return mPos.mX;
	}

	//---------------------------------------------------
	float DxGraphicComponent::getY() const
	{
		return mPos.mY;
	}

	//---------------------------------------------------
	void DxGraphicComponent::setDirection(float rotation)
	{
		mRotation = rotation;
	}

	//---------------------------------------------------
	void DxGraphicComponent::setVisibility(const bool visible)
	{
		mVisibility = visible;
	}

	//---------------------------------------------------
	bool DxGraphicComponent::isVisible() const
	{
		return mVisibility;
	}

	//---------------------------------------------------
	void DxGraphicComponent::setLayer(const float layer)
	{
		SpriteRenderer::GetSingleton()->remove(this);
		mLayer = layer;
		SpriteRenderer::GetSingleton()->add(this);
	}

	//---------------------------------------------------
	float DxGraphicComponent::getLayer() const
	{
		return mLayer;
	}

	//---------------------------------------------------
	void DxGraphicComponent::tick(const float deltaTime)
	{
	}

	//---------------------------------------------------
	ID3D11Buffer* DxGraphicComponent::createVertexBuffer(ID3D11Device *device, const UINT size, const void *data) const
	{
		D3D11_BUFFER_DESC bDesc;
		bDesc.Usage = D3D11_USAGE_IMMUTABLE;
		bDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		bDesc.CPUAccessFlags = 0;
		bDesc.MiscFlags = 0;
		bDesc.StructureByteStride = 0;
		bDesc.ByteWidth = size;
		D3D11_SUBRESOURCE_DATA dDesc;

		dDesc.pSysMem = data;
		dDesc.SysMemPitch = size;
		dDesc.SysMemSlicePitch = 0;
		ID3D11Buffer *b = 0;
		HRESULT res = device->CreateBuffer(&bDesc, &dDesc, &b);
		if(res != S_OK)
			throw ElUnableToCreateBuffers;
		return b;
	}

	//---------------------------------------------------
	ID3D11Buffer* DxGraphicComponent::createIndexBuffer(ID3D11Device *device, const UINT size, const void *data) const
	{
		D3D11_BUFFER_DESC bDesc;
		bDesc.Usage = D3D11_USAGE_IMMUTABLE;
		bDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
		bDesc.CPUAccessFlags = 0;
		bDesc.MiscFlags = 0;
		bDesc.StructureByteStride = 0;
		bDesc.ByteWidth = size;
		D3D11_SUBRESOURCE_DATA dDesc;

		dDesc.pSysMem = data;
		dDesc.SysMemPitch = size;
		dDesc.SysMemSlicePitch = 0;
		ID3D11Buffer *b = 0;
		HRESULT res = device->CreateBuffer(&bDesc, &dDesc, &b);
		if(res != S_OK)
			throw ElUnableToCreateBuffers;
		return b;
	}
}
